/**
 * fileName:  RegionDictBean.java
 * author:  lr
 * Company:  Cloudland Studio
 * Createdate:  2014-7-8 下午04:38:05
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-7-8          lr            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.ArrayList;
import java.util.List;

import org.cloudland.commercial.masterdata.bean.FruitCategoryInfo;
import org.cloudland.commercial.masterdata.bean.RegionDictInfo;
import org.cloudland.commercial.masterdata.store.entity.CategoryEntity;
import org.cloudland.commercial.masterdata.store.entity.RegionEntity;
import org.cloudland.commercial.masterdata.store.read.RegionDictReader;
import org.cloudland.commercial.masterdata.store.write.RegionDictWriter;
import org.cloudland.jdbc.ibatis.masterdata.entity.RegionDictEntity;

/**
 * <pre>
 * 国家地区实现类
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * 
 * @ClassName RegionDictBean
 * @author lr
 * @version %I%, %G%
 * @see
 * @since JDK1.6
 * 
 */
public class RegionDictBean implements RegionDictInfo {

	/**
	 * regionEntity 地区字典信息
	 */
	private RegionEntity regionEntity;

	/**
	 * childRegionInfo 子节点信息
	 */
	private List<RegionDictInfo> childRegionInfo = new ArrayList<RegionDictInfo>();

	/**
	 * regionDictWriter 地区更新操作
	 */
	private RegionDictWriter regionDictWriter;

	/**
	 * regionDictReader 地区查询操作
	 */
	private RegionDictReader regionDictReader;

	/**
	 * <pre>
	 * 构造方法
	 * </pre>
	 * 
	 * @param regionEntity
	 * @param regionDictWriter
	 * @param regionDictReader
	 */
	public RegionDictBean(RegionEntity regionEntity,
			RegionDictWriter regionDictWriter, RegionDictReader regionDictReader) {
		this.regionEntity = regionEntity;
		this.regionDictWriter = regionDictWriter;
		this.regionDictReader = regionDictReader;
	}

	public RegionDictBean(RegionDictWriter regionDictWriter,
			RegionDictReader regionDictReader) {
		this(new RegionEntity(), regionDictWriter, regionDictReader);
	}

	/**
	 * @return
	 */
	public String getRegionId() {
		return regionEntity.getRegionId();
	}

	/**
	 * @return
	 */
	public String getRegionName() {
		return regionEntity.getRegionName();
	}

	/**
	 * @return
	 */
	public Integer getSort() {
		return regionEntity.getSort();
	}

	/**
	 * @return
	 */
	public RegionDictInfo[] getChilds() {
		
		List<RegionEntity> regionDictList = regionDictReader.findChildRegionById(getRegionId());
		
		if (null == regionDictList || regionDictList.size() < 1) {
			return new RegionDictInfo[0];
		}

		if (regionDictList.size() > 0) {
			childRegionInfo = new ArrayList<RegionDictInfo>(
					regionDictList.size());
		}

		for (RegionEntity entity : regionDictList) {
			childRegionInfo.add(new RegionDictBean(entity,this.regionDictWriter, this.regionDictReader));
		}

		return childRegionInfo.toArray(new RegionDictInfo[childRegionInfo.size()]);

	}

	/**
	 * @param regionName
	 */
	public void setRegionName(String regionName) {
		regionEntity.setRegionName(regionName);
	}

	/**
	 * @param sort
	 */
	public void setSort(Integer sort) {
		regionEntity.setSort(sort);
	}

	/**
	 * <pre>
	 * TODO 请充分描述该方法提供的功能，及如何使用。
	 * </pre>
	 * 
	 * @param regionParentId
	 */
	public void setRegionParentId(String regionParentId) {
		regionEntity.setRegionParentId(regionParentId);
	}

	/**
	 * @param regionDictInfo
	 */
	public void addChild(RegionDictInfo regionDictInfo) {
		childRegionInfo.add(regionDictInfo);
	}

	/**
	 * 
	 */
	public void submit() {
		if (null != regionEntity.getRegionId() && regionEntity.getRegionId().length() > 0) {
			this.regionDictWriter.update(regionEntity);
		} else {
			this.regionDictWriter.save(regionEntity);
		}

		// 子分类不为空, 保存子分类
		for (RegionDictInfo child : childRegionInfo) {
			((RegionDictBean) child).setRegionParentId(this.getRegionId());
			child.submit();
		}
		
	}

}
